package org.system.core.controller.system;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.system.core.annotation.Log;
import org.system.core.common.PageResult;
import org.system.core.common.Result;
import org.system.core.domain.entity.SysPost;
import org.system.core.enums.BusinessType;
import org.system.core.service.system.SysPostService;
import org.system.core.utils.PageUtils;

import java.util.List;

/**
 * @author baoRoot
 */
@Api(tags = {"岗位模块"})
@RestController
@RequestMapping("system/post")
public class SysPostController {

    private static final Logger logger = LoggerFactory.getLogger(SysPostController.class);

    @Autowired
    private SysPostService sysPostService;

    @GetMapping("/list")
    @ApiOperation("获取岗位列表")
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    public PageResult<SysPost> list(SysPost sysPost) {
        logger.info("获取岗位列表： {}", sysPost);
        PageUtils.startPage();
        PageInfo<SysPost> pageInfo = new PageInfo<>(sysPostService.selectPostList(sysPost));
        return PageResult.ok(pageInfo);
    }

    @GetMapping("/{postId}")
    @ApiOperation("根据岗位id获取岗位详细信息")
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    public Result<SysPost> getPostInfo(@PathVariable Long postId) {
        logger.info("根据岗位id获取岗位详细信息: {}", postId);
        return Result.ok(sysPostService.selectPostById(postId));
    }

    @PostMapping
    @ApiOperation("新增岗位")
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    public Result<String> addPost(@Validated @RequestBody SysPost post) {
        logger.info("新增岗位: {}", post);
        sysPostService.insertPost(post);
        return Result.ok();
    }

    @PutMapping
    @ApiOperation("更新岗位")
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    public Result<String> editPost(@Validated @RequestBody SysPost post) {
        logger.info("更新岗位: {}", post);
        sysPostService.updatePost(post);
        return Result.ok();
    }

    @ApiOperation("删除岗位")
    @DeleteMapping("/{postIds}")
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    public Result<String> deletePost(@PathVariable Long[] postIds) {
        logger.info("删除岗位： {}", postIds);
        sysPostService.deletePost(postIds);
        return Result.ok();
    }

    @ApiOperation("岗位选择列表框")
    @GetMapping("optionSelect")
    public Result<List<SysPost>> optionsSelect() {
        logger.info("岗位选择列表框");
        return Result.ok(sysPostService.selectPostAll());
    }

}
